var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "components/common/component", "react", "components/common/spiner-component", "mobile/services/shoppingService", "maishu-ui-toolkit"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const component_1 = require("components/common/component");
    const React = require("react");
    const spiner_component_1 = require("components/common/spiner-component");
    const shoppingService_1 = require("mobile/services/shoppingService");
    const maishu_ui_toolkit_1 = require("maishu-ui-toolkit");
    let StoreCoupons = class StoreCoupons extends spiner_component_1.SpinerComponent {
        constructor(props) {
            super(props);
            this.shopping = this.createService(shoppingService_1.ShoppingService);
        }
        loadData() {
            return __awaiter(this, void 0, void 0, function* () {
                let coupons = yield this.shopping.storeCoupons();
                return { coupons };
            });
        }
        render() {
            let { coupons } = this.state;
            return React.createElement(React.Fragment, null,
                coupons.length > 0 ?
                    React.createElement("hr", null)
                    :
                        React.createElement("div", { className: "norecords" },
                            React.createElement("div", { className: "icon" },
                                React.createElement("i", { className: "icon-money" })),
                            React.createElement("h4", { className: "text" }, "\u6682\u65E0\u53EF\u9886\u53D6\u7684\u4F18\u60E0\u5238")),
                coupons.map(o => React.createElement("div", { key: o.Id },
                    React.createElement(CouponControl, { page: this.page, coupon: o }),
                    React.createElement("hr", null))));
        }
    };
    StoreCoupons = __decorate([
        component_1.component()
    ], StoreCoupons);
    exports.StoreCoupons = StoreCoupons;
    class CouponControl extends React.Component {
        receiveCoupon(coupon) {
            let shopping = this.props.page.createService(shoppingService_1.ShoppingService);
            return shopping.receiveCoupon(coupon.Id);
        }
        formatDate(date) {
            let d = date;
            let mm = d.getMonth() + 1;
            let dd = d.getDate();
            return `${mm < 10 ? '0' + mm : mm}/${dd < 10 ? '0' + dd : dd}`;
        }
        render() {
            let o = this.props.coupon;
            return React.createElement("div", { className: "coupon" },
                React.createElement("button", { className: `pull-right receive available`, ref: (btn) => {
                        if (!btn)
                            return;
                        maishu_ui_toolkit_1.buttonOnClick(btn, () => {
                            return this.receiveCoupon(o);
                        }, { toast: '领取优惠劵成功' });
                    } }, "\u7ACB\u5373\u9886\u53D6"),
                React.createElement("div", { className: "main" },
                    React.createElement("div", null,
                        "\uFFE5",
                        o.Discount),
                    React.createElement("div", { className: "date" },
                        `有效期 ${o.ValidBegin ? this.formatDate(o.ValidBegin) : ''} 至 ${o.ValidEnd ? this.formatDate(o.ValidEnd) : ''}`,
                        React.createElement("div", null, o.Amount ? React.createElement("span", null,
                            "\u6EE1",
                            o.Amount,
                            "\u5143\u53EF\u4EE5\u4F7F\u7528") : React.createElement("span", null, "\u4EFB\u610F\u91D1\u989D\u53EF\u7528\u4F7F\u7528")))));
        }
    }
    exports.CouponControl = CouponControl;
});
